#!/usr/bin/perl

#
#   make_schemas.pl
#
#   Automatically creates schemas.xml and English.lproj/schema.strings
#   based on the contents of FITS_keywords.txt
#
#   Spotlight metadata is created in the form of 
#       org_iau_FITS_<KEYNAME> where <KEYNAME> is a FITS keyword.
#
#
#   History:
#       2005-05-03    Created by Marshall Perrin <mperrin@astro.berkeley.edu>
#
#

open KEYWORDS,"FITS_keywords.txt" or die "could not open: $!";

@keywords = <KEYWORDS>;

close KEYWORDS;

#  Create English localized strings
# Xcode requires schema files to be in UTF-16. 
open SCHEMA,">:encoding(UTF-16)", "./English.lproj/schema.strings" or die "could not open: $!";

print SCHEMA <<HERE;

    /* Localized DisplayNames for your custom metadata attributes */
    /* The form is:  "myMetadataKey" = "My Metadata Key";    */
    /* The name on the left is the name defined in schema.xml */
    /* The name on the right is what the user will see */

    /* Localized Description of you custom metadata attributes */
    /* The form is: "myMetadataKey.Description" = "stuff";
    /* com_Foo_YourAttrName should be definded in the schema.xml file  */
    /* The value on the right will be displayed by the Finder etc.. */

HERE

foreach $line (@keywords) {
    chomp $line;
    ($k,$desc) = split(/\t+/,$line);
    $key = makekey($k);
    print SCHEMA <<HERE;
"$key" = "FITS $desc";
"$key.Description" = "FITS $desc";
HERE

}

close SCHEMA;

# Create schema.xml file
open SCHEMAXML,">./schema.xml" or die "could not open: $!";

print SCHEMAXML<<HERE;
<?xml version="1.0" encoding="UTF-8"?>
 
<schema version="1.0" xmlns="http://www.apple.com/metadata"
                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      xsi:schemaLocation="http://www.apple.com/metadata file:///System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Resources/MetadataSchema.xsd">
    <note>
        Custom attributes that this metadata importer supports.  Below
    is an example of a multivalued string attribute.  Other types
    are CFNumber, CFDate, CFBoolean and CFData.               
    </note>
    <attributes>
HERE

foreach $line (@keywords) {
    chomp $line;
    ($k,$desc,$type) = split(/\t+/,$line);
    $key = makekey($k);
    push @keylist, $key;
    print SCHEMAXML <<HERE;
        <attribute name="$key" multivalued="false" type="$type"/>
HERE

}
print SCHEMAXML <<HERE;
    </attributes>

    <types>
        <type name="org.iau.fits">
            <note>
            The keys that this metadata importer handles.
            </note>
            <allattrs>
            @keylist
            </allattrs>
            <displayattrs>
            @keylist
            </displayattrs>
        </type>
    </types>
</schema>
HERE

close SCHEMAXML;



sub makekey {
    my $key = shift;

    $key =~ s/[-.]/_/g;
    return "org_iau_FITS_$key";
}

